package com.ciaojian.store.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.enums.business.WorkerApplyStatusEnum;
import com.ciaojian.core.exception.EntityNotFoundException;
import com.ciaojian.core.model.WorkerApply;
import com.ciaojian.core.model.dto.WorkerApplyDTO;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.service.WorkerApplyServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Optional;

/**
 * @author Atlakyn
 */
@Api(tags = "发型师: 考勤模块")
@Validated
@RestController
@RequestMapping("/attendance")
public class AttendanceController {
    @Resource
    private WorkerApplyServiceImpl workerApplyService;


    @ApiOperation("发型师申请列表")
    @GetMapping("/worker-apply")
    public Result getWorkerApply(WorkerApplyDTO params,
                                 @RequestParam(defaultValue = "0") Integer pageNo,
                                 @RequestParam(defaultValue = "6") Integer pageSize) {
        Page<Map<String, Object>> page = workerApplyService.listByStatus(params, new Page<>(pageNo, pageSize));
        return Result.ok(page);
    }

    @ApiOperation("申请详情")
    @GetMapping("/worker-apply/{workerApplyId}")
    public Result getWorkerApply(@PathVariable Integer workerApplyId) {
        WorkerApply workerApply = Optional.of(new WorkerApply().selectById(workerApplyId))
                .orElseThrow(() -> new EntityNotFoundException(WorkerApply.class, "id", workerApplyId));
        return Result.ok(workerApply);
    }

    @ApiOperation("申请审核")
    @Transactional(rollbackFor = Exception.class)
    @PutMapping("/worker-apply/audit")
    public Result auditWorkerApply(@NotNull Integer workerApplyId,
                                   @NotNull WorkerApplyStatusEnum status,
                                   String disallowReason) {
        WorkerApply workerApply = Optional.of(new WorkerApply().selectById(workerApplyId))
                .orElseThrow(() -> new EntityNotFoundException(WorkerApply.class, "id", workerApplyId));
        Assert.isTrue(workerApply.getStatus().equals(WorkerApplyStatusEnum.WAIT), "状态异常");
        workerApplyService.auditLeave(workerApply, status, disallowReason);
        return Result.ok();
    }
}